package com.sgb.goods.view.activity.details.order;

import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsActivityPayInformationBinding;
import com.sgb.goods.entity.order.OrderPayDetailEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.BaseActivity;
import com.sgb.goods.viewmodel.PayInformationViewModel;
import com.sgb.goods.widget.pop.PaymentAgreementPop;
import com.sgb.goods.widget.pop.PaymentDetailsPop;

import java.util.Objects;

/**
 * 采购订单详情--支付信息
 *
 * @author cyj
 */
@Route(path = Constants.ARouter.AR_PAY_INFORMATION)
public class PayInformationActivity extends BaseActivity<GoodsActivityPayInformationBinding, PayInformationViewModel> {

    private PaymentDetailsPop paymentDetailsPop;
    private PaymentAgreementPop paymentAgreementPop;
    private String orderId;
    /**
     * 订单类型 默认 1：企业下单 0 个人下单 2 团体下单
     */
    private int orderIdentity;

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_activity_pay_information;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(PayInformationViewModel.class, BR.payInformationViewModel);
    }

    @Override
    protected void init() {
        this.orderId = getIntent().getStringExtra("orderId");
        this.orderIdentity = getIntent().getIntExtra("orderIdentity", 1);
        int orderProcessState = getIntent().getIntExtra("orderProcessState", 1);
        String orderType = getIntent().getStringExtra("orderType");
        int paymentMethod = getIntent().getIntExtra("paymentMethod", 0);
        mViewModel.orderType.set(orderType);
        mDataBinding.head.title.setText("2".equals(mViewModel.orderType.get()) ? "收款信息" : "付款信息");
        mDataBinding.goodsTvPayment.setText(1 == getIntent().getIntExtra("paymentWay", 1) ? "货款模式：先款后货" : "货款模式：先货后款");
        mDataBinding.goodsTvPaymentAgreement.setVisibility((orderProcessState == 3 || orderProcessState == 4) ? View.VISIBLE : View.GONE);

        DividerItemDecoration divider = new DividerItemDecoration(PayInformationActivity.this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(Objects.requireNonNull(ContextCompat.getDrawable(PayInformationActivity.this, R.drawable.goods_divider_item_decoration_5)));
        mDataBinding.rlvView2.addItemDecoration(divider);

        //销售订单的待接单或者采购（销售）订单待确认状态 列表数据显示约定信息
        if (2 == orderProcessState || (orderProcessState == 1 && "2".equals(mViewModel.orderType.get()))) {
            mDataBinding.rlvView2.setAdapter(mViewModel.payInformationAdapter);
            mViewModel.getPaymentAgreementList(orderId, false);
        } else { //列表数据显示付（收）款信息
            if (paymentMethod == 1) {
                mDataBinding.goodsBankInfoContainer.setVisibility(View.VISIBLE);
            }
            mDataBinding.rlvView2.setAdapter(mViewModel.adapter);
            mViewModel.getListPayDetails(orderId, orderType, paymentMethod, orderProcessState);
        }
    }

    @Override
    protected void initObserve() {
        mViewModel.paymentDetailsList.observe(this, entity -> {
            // 付款详情弹窗
            if (null == paymentDetailsPop) {
                paymentDetailsPop = new PaymentDetailsPop(PayInformationActivity.this);
                paymentDetailsPop.setBgView(mDataBinding.grayView);
            }
            paymentDetailsPop.showPop(mDataBinding.grayView, entity, "2".equals(mViewModel.orderType.get()));
        });
        mViewModel.orderPayDetailEntity.observe(this, orderPayDetailEntities -> {
            // 付款约定弹窗
            if (null == paymentAgreementPop) {
                paymentAgreementPop = new PaymentAgreementPop(PayInformationActivity.this);
                paymentAgreementPop.setBgView(mDataBinding.grayView);
            }
            paymentAgreementPop.showPop(mDataBinding.grayView, orderPayDetailEntities);
        });

        mViewModel.orderPayAgreementEntity.observe(this, orderPayAgreementEntities -> {
            if (orderPayAgreementEntities == null || orderPayAgreementEntities.size() == 0) {
                mViewModel.showNoData(mDataBinding.recyclerViewContainer);
            }
        });

        mViewModel.cOrderPayDetailEntity.observe(this, orderPayDetailEntities -> {
            if (orderPayDetailEntities == null || orderPayDetailEntities.size() == 0) {
                mViewModel.showNoData(mDataBinding.recyclerViewContainer);
            }
        });
    }

    @Override
    protected void initListener() {
        mDataBinding.head.ivBack.setOnClickListener(v -> finish());
        mDataBinding.goodsTvPaymentAgreement.setOnClickListener(v -> mViewModel.getPaymentAgreementList(orderId, true));
        mViewModel.adapter.setOnItemChildClickListener((adapter, view, position) -> {
            // 支付信息-付款详情（点击事件）
            OrderPayDetailEntity entity = (OrderPayDetailEntity) view.getTag();
            String payStateDesc = entity.getPayStateString();
            mViewModel.getPaymentDetails(entity.id, payStateDesc, orderIdentity);
        });

    }
}